package com.sunny.shop.api.v1;

import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import com.sunny.shop.core.interceptors.ScopeLevel;
import com.sunny.shop.model.Category;
import com.sunny.shop.model.Song;
import com.sunny.shop.service.SongServiceImpl;
import com.sunny.shop.vo.CategorySongVO;
import com.sunny.shop.vo.SongSimplifyVO;
import com.sunny.shop.vo.SongVO;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/song")
public class SongController {
    @Autowired
    private SongServiceImpl songService;

    /**
     * 获取分类
     * @return
     */
    @GetMapping("/category")
    public ArrayList<CategorySongVO> getCateList(){
        Mapper mapper = DozerBeanMapperBuilder.buildDefault();
        Long pid = 2l;
        List<Category> cateList = songService.getCateList(pid);
        ArrayList<CategorySongVO> vos = new ArrayList<>();
        HashMap<Integer, String> colorMap = new HashMap<>();
        colorMap.put(44,"#ff6e8d-#FEA990");
        colorMap.put(45,"#fc86ee-#EAADFA");
        colorMap.put(46,"#968AF8-#8DCEF9");
        colorMap.put(47,"#32CCBC-#9CDF85");
        cateList.forEach(item -> {
            CategorySongVO map = mapper.map(item, CategorySongVO.class);
            String color = colorMap.get((item.getId()).intValue());
            map.setColor(color);
            vos.add(map);
        });

        return vos;
    }

    /**
     * 获取滚动列表
     * @return
     */
    @GetMapping("")
    public List<List<SongSimplifyVO>> getSongList(){
        List<Song> songList = songService.getSongList();
        List<SongSimplifyVO> songSimplifyVO = new ArrayList<>();
        Mapper mapper = DozerBeanMapperBuilder.buildDefault();
        songList.forEach(item->{
            SongSimplifyVO map = mapper.map(item, SongSimplifyVO.class);
            songSimplifyVO.add(map);
        });
        List<List<SongSimplifyVO>> partitions = ListUtils.partition(songSimplifyVO,10);
        return partitions;
    }

    @GetMapping("/{id}")
    @ScopeLevel()
    public ArrayList<SongVO> getSongById(@PathVariable Long id){
        ArrayList<SongVO> result = new ArrayList<>();
        SongVO song = songService.getSongById(id);
        result.add(song);
        return result;
    }

    @ScopeLevel
    @GetMapping("/cate/{cate_id}")
    public ArrayList<SongVO> getSongByCateId(@PathVariable Long cate_id){
        ArrayList<SongVO> song = songService.getSongByCateId(cate_id);
        return song;
    }
}
